import 'dart:io';

import 'package:boss/color_constant.dart';
import 'package:boss/ui/widget/image/base_image.dart';
import 'package:boss/ui/widget/picker/imagePicker/image_picker.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';

class UploadImage extends StatefulWidget {
  final String groundImage;
  final String uploadedImage;
  final double width;
  final double height;
  final ValueChanged<String> onUploaded;
  final ValueChanged<File> onPickedImage;
  final bool enabled;

  const UploadImage({
    Key key,
    this.groundImage,
    this.width,
    this.height,
    this.onUploaded,
    this.uploadedImage,
    this.onPickedImage,
    this.enabled = true,
  }) : super(key: key);

  @override
  _UploadImageState createState() => _UploadImageState();
}

class _UploadImageState extends State<UploadImage> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: widget.enabled
          ? () async {
              final image = await ImagePicker.pickImage(context);
              if (image != null) {
                if (widget.onPickedImage != null) {
                  widget.onPickedImage(image);
                }

                var imageUrl = await UploadUtil.uploadImage(image);
                if (imageUrl != null && widget.onUploaded != null) {
                  widget.onUploaded(imageUrl);
                }
              }
            }
          : null,
      child: Container(
        width: widget.width,
        height: widget.height,
        decoration: BoxDecoration(
          color: ColorConstant.e5e5,
          borderRadius: BorderRadius.circular(10.0),
          image: widget.uploadedImage == null || widget.uploadedImage == ''
              ? null
              : DecorationImage(
                  image: NetworkImage(widget.uploadedImage),
                  fit: BoxFit.fill,
                ),
        ),
        child: Center(
          child: widget.uploadedImage == null || widget.uploadedImage == ''
              ? BaseImage(
                  'assets/image/camera_light.png',
                  width: 80.w,
                  height: 80.w,
                )
              : null,
        ),
      ),
    );
  }
}
